/* 
 * @(#)WCDialog.java    Created on 2015-3-25
 * Copyright (c) 2015 ZDSoft Networks, Inc. All rights reserved.
 * $Id$
 */
package com.xuan.wcdialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 自定义Dialog
 * 
 * @author xuan
 * @version $Revision: 1.0 $, $Date: 2015-3-25 下午5:37:29 $
 */
public class WCDialog extends Dialog {
    private Activity activity;

    protected TextView titleTextView;
    protected TextView messageTextView;
    protected EditText inputEdit;

    protected Button leftBtn;
    protected View btnSeperator;
    protected Button rightBtn;

    protected Button oneBtn;

    protected View contentView;

    public WCDialog(Context context) {
        super(context);
        activity = (Activity) context;

        contentView = LayoutInflater.from(activity).inflate(R.layout.wc_dialog, null);
        titleTextView = (TextView) contentView.findViewById(R.id.titleTextView);
        messageTextView = (TextView) contentView.findViewById(R.id.messageTextView);
        inputEdit = (EditText) contentView.findViewById(R.id.inputEdit);
        leftBtn = (Button) contentView.findViewById(R.id.leftBtn);
        btnSeperator = contentView.findViewById(R.id.btnSeperator);
        rightBtn = (Button) contentView.findViewById(R.id.rightBtn);
        oneBtn = (Button) contentView.findViewById(R.id.oneBtn);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));// 背景透明
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);// 不需要标题
        setContentView(contentView);
    }

    @Override
    public void show() {
        super.show();
        WindowManager windowManager = activity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = (display.getWidth() - dp2px(20)); // 设置宽度
        this.getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);
    }

    private int dp2px(int dp) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return (int) (metrics.density * dp);
    }

}
